﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.DBManager;
using DelonixRegiaHotel.Classes;

namespace DelonixRegiaHotel
{
    public partial class ManageBooking : Form
    {
        ArrayList bookingDataInfo = new ArrayList();
        
        public ManageBooking()
        {
            InitializeComponent();
            if(checkIfItemsExist() == true)
            {
                return;
            }
            bookingDataInfo = AdvBookingDBManager.GetAllCheckInDetails();
            dgvbookingdetails.DataSource = bookingDataInfo;
            removeColumnName();
            ArrayList tempHolder = new ArrayList();
            ArrayList tempHolder2 = new ArrayList();
            tempHolder.Add("-Select Check ID-");
            tempHolder2 = AdvBookingDBManager.GetAllCheckInDetails();

            foreach (CheckInOut c in tempHolder2)
            {
                tempHolder.Add(c.CheckID.ToString());
            }
            cboxcheckid.DataSource = tempHolder;
        }

        Form mdiChildForm = new Form();
        public void Form1_Load(object sender, System.EventArgs e)
        {
            MdiClient ctlMDI;

            foreach (Control ctl in MainForm.ActiveForm.Controls)
            {
                try
                {
                    // Attempt to cast the control to type MdiClient.
                    ctlMDI = (MdiClient)ctl;

                    // Set the BackColor of the MdiClient control.
                    ctlMDI.BackColor = this.BackColor;
                }
                catch (InvalidCastException exc)
                {
                    // Catch and ignore the error if casting failed.
                }
            }

            // Set the IsMdiContainer property to true.
            IsMdiContainer = true;

            // Set the child form's MdiParent property to  
            // the current form.
            mdiChildForm.MdiParent = MainForm.ActiveForm;

        }

        public void callForm(Form newForm)
        {
            if (CheckOpened(newForm.Text) == false)
            {
                newForm.MdiParent = MainForm.ActiveForm;
                // Display the new form.
                newForm.StartPosition = FormStartPosition.CenterScreen;
                newForm.Show();
            }
        }

        public void hide_form()
        {
            this.Visible = false;
        }

        private void alltextbox_TextChanged(object sender, EventArgs e)
        {
            ArrayList tempHolder = new ArrayList();
            foreach (CheckInOut c in bookingDataInfo)
            {
                if (c.CheckID.ToString().IndexOf(tbxcheckid.Text, 0, StringComparison.CurrentCultureIgnoreCase) != -1 && c.PaymentType.ToString().IndexOf(tbxpaymenttype.Text, 0, StringComparison.CurrentCultureIgnoreCase) != -1 && c.RoomNo.ToString().IndexOf(tbxrmno.Text, 0, StringComparison.CurrentCultureIgnoreCase) != -1
                    && c.CheckInTime.ToString().IndexOf(tbxcheckindatetime.Text, 0, StringComparison.CurrentCultureIgnoreCase) != -1 && c.CheckOutTime.ToString().IndexOf(tbxcheckoutdatetime.Text, 0, StringComparison.CurrentCultureIgnoreCase) != -1 && c.CustomerID.ToString().IndexOf(tbxcustomerid.Text, 0, StringComparison.CurrentCultureIgnoreCase) != -1)
                {
                    tempHolder.Add(c);
                }
            }
            dgvbookingdetails.DataSource = tempHolder;
        }

        private void btnfliter_Click(object sender, EventArgs e)
        {
            tbxcheckid.Text = "";
            tbxcheckindatetime.Text = "";
            tbxcheckoutdatetime.Text = "";
            tbxcustomerid.Text = "";
            tbxpaymenttype.Text = "";
            tbxrmno.Text = "";
        }

        private void btncreate_Click(object sender, EventArgs e)
        {
            FormCollection fc = Application.OpenForms;
            foreach(Form frm in fc)
            {
                if (CheckOpened("UpdateBooking") == true)
                {
                    MessageBox.Show("you can only open either create booking or update booking 1 at a time only");
                    return;
                }
            }
            
            hide_form();
            CreateBooking newMDIChild = new CreateBooking();
            // Set the Parent Form of the Child window.
            callForm(newMDIChild);
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            //2 methods to check and stop if there is need to
            if(checkIfItemsExist() == true) //stop if there are already no items to delete
            {
                MessageBox.Show("There are no items to delete");
                return;
            }

            if (CheckOpened("CreateBooking") == true || CheckOpened("UpdateBooking")) //stop if create or update form is opened to prevent any conflict such as updating of information that does not exist
            {
                MessageBox.Show("kindly close the create or update booking form to proceed with delete");
                return;
            }
            // end of 2 methods

            if (cboxcheckid.SelectedIndex == 0)
            {
                MessageBox.Show("Please at least select 1 checkID to delete");
            }

            else
            {
                if ((MessageBox.Show("Are you sure you want to delete", "Confirm delete?", MessageBoxButtons.YesNo) == DialogResult.Yes))
                {
                    AdvBookingDBManager.DeleteBookingByID(cboxcheckid.SelectedItem.ToString());
                    ArrayList tempHolder = new ArrayList();
                    ArrayList tempHolder2 = new ArrayList();
                    tempHolder.Add("-Select Check ID-");
                    tempHolder2 = AdvBookingDBManager.GetAllCheckInDetails();

                    foreach (CheckInOut c in tempHolder2)
                    {
                        tempHolder.Add(c.CheckID.ToString());
                    }
                    cboxcheckid.DataSource = tempHolder;
                    bookingDataInfo = AdvBookingDBManager.GetAllCheckInDetails();
                    dgvbookingdetails.DataSource = bookingDataInfo;
                }
                else
                {
                    return;
                }
            }
        }

        public void removeColumnName()
        {
            if (checkIfItemsExist() == true)
            {
                return;
            }
            dgvbookingdetails.Columns["Deposit"].Visible = false;
            dgvbookingdetails.Columns["balance"].Visible = false;
            dgvbookingdetails.Columns["numOfAdult"].Visible = false;
            dgvbookingdetails.Columns["numOfChild"].Visible = false;
            dgvbookingdetails.Columns["numOfKey"].Visible = false;
            dgvbookingdetails.Columns["extraCostTotal"].Visible = false;
            dgvbookingdetails.Columns["roomRate"].Visible = false;
            dgvbookingdetails.Columns["remarks"].Visible = false;
            dgvbookingdetails.Columns["numOfDays"].Visible = false;
            dgvbookingdetails.Columns["roomStatus"].Visible = false;
        }

        public bool checkIfItemsExist()
        {
            bookingDataInfo = AdvBookingDBManager.GetAllCheckInDetails();
            if (bookingDataInfo.Count == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            FormCollection fc = Application.OpenForms;
            foreach (Form frm in fc)
            {
                if (CheckOpened("CreateBooking") == true)
                {
                    MessageBox.Show("you can only open either create booking or update booking 1 at a time only");
                    return;
                }
            }
            if(cboxcheckid.SelectedIndex == 0)
            {
                MessageBox.Show("please select at least 1 checkID to update");
                return;   
            }

            UpdateBooking newMDIChild = new UpdateBooking(cboxcheckid.SelectedItem.ToString());
            callForm(newMDIChild);
            hide_form();
        }

        public bool CheckOpened(string name)
        {
            FormCollection fc = Application.OpenForms;
            foreach (Form frm in fc)
            {
                if (frm.Text == name)
                {
                    return true;
                }
            }
            return false;
        }

    }
}
